package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R save(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("添加分类成功");
    }

    /**
     * 菜品查看分类
     *
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R page(PageParam pageParam) {
        IPage<Category> page = new Page<>(pageParam.getPage(), pageParam.getPageSize());
        categoryService.page(page);
        return R.success(page);
    }

    /**
     * 删除分类
     *
     * @return
     */
    @DeleteMapping
    public R delete(Long id) {
        int remove = categoryService.remove(id);
        if (remove > 0) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 根据id修改
     *
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改信息成功");
    }

    /**
     * 根据类型查找分类
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R list(int type) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getType, type);
        lqw.orderByDesc(Category::getUptateTable).orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }


}
